package com.vt.common.core.config;

import java.text.*;
import java.util.Date;

/**
 * @Auther: PanZha
 * @Date: 2018/10/25 13:39
 */
public class CtmDateFormat extends DateFormat {

    private SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");

    private DateFormat dateFormat;

    public CtmDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return new StringBuffer(format1.format(date));
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        Date date;
        try {

            date = format1.parse(source, pos);
        } catch (Exception e) {

            date = dateFormat.parse(source, pos);
        }

        return date;
    }

    @Override
    public Date parse(String source) throws ParseException {
        Date date;
        try {
            date = format1.parse(source);
        } catch (Exception e) {
            date = dateFormat.parse(source);
        }

        return date;
    }

    @Override
    public Object clone() {
        Object format = dateFormat.clone();
        return new CtmDateFormat((DateFormat) format);
    }
}
